package com.cuz.daileetcode;

/**
 * @author cuzz
 * @version 1.0
 * @description: 23. 合并K个升序链表
 * 给你一个链表数组，每个链表都已经按升序排列。
 * <p>
 * 请你将所有链表合并到一个升序链表中，返回合并后的链表。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入：lists = [[1,4,5],[1,3,4],[2,6]]
 * 输出：[1,1,2,3,4,4,5,6]
 * 解释：链表数组如下：
 * [
 * 1->4->5,
 * 1->3->4,
 * 2->6
 * ]
 * 将它们合并到一个有序链表中得到。
 * 1->1->2->3->4->4->5->6
 * 示例 2：
 * <p>
 * 输入：lists = []
 * 输出：[]
 * 示例 3：
 * <p>
 * 输入：lists = [[]]
 * 输出：[]
 * <p>
 * <p>
 * 提示：
 * <p>
 * k == lists.length
 * 0 <= k <= 10^4
 * 0 <= lists[i].length <= 500
 * -10^4 <= lists[i][j] <= 10^4
 * lists[i] 按 升序 排列
 * lists[i].length 的总和不超过 10^4
 * @date 21:38 2021/9/25
 **/
public class Day8_2 {
    static class S1 {
        private static class ListNode {
            int val;
            ListNode next;

            ListNode() {
            }

            ListNode(int val) {
                this.val = val;
            }

            ListNode(int val, ListNode next) {
                this.val = val;
                this.next = next;
            }
        }

        public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null) {
                return l2;
            }
            if (l2 == null) {
                return l1;
            }
            //哑节点方便编码
            ListNode head = new ListNode(0);
            ListNode now = head;
            while (l1 != null && l2 != null) {
                if (l1.val <= l2.val) {
                    now.next = l1;
                    l1 = l1.next;
                } else {
                    now.next = l2;
                    l2 = l2.next;
                }
                now = now.next;
            }
            now.next = l1 == null ? l2 : l1;
            return head.next;
        }

        public static ListNode mergeKLists(ListNode[] lists) {

            ListNode res = null;
            for (int index = 0; index < lists.length; index++) {
                res = mergeTwoLists(res, lists[index]);
            }
            return res;
        }
    }

    private static class S2 {
        private static  class ListNode {
            int val;
            ListNode next;

            ListNode() {
            }

            ListNode(int val) {
                this.val = val;
            }

            ListNode(int val, ListNode next) {
                this.val = val;
                this.next = next;
            }
        }

        public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null) {
                return l2;
            }
            if (l2 == null) {
                return l1;
            }
            //哑节点方便编码
            ListNode head = new ListNode(0);
            ListNode now = head;
            while (l1 != null && l2 != null) {
                if (l1.val <= l2.val) {
                    now.next = l1;
                    l1 = l1.next;
                } else {
                    now.next = l2;
                    l2 = l2.next;
                }
                now = now.next;
            }
            now.next = l1 == null ? l2 : l1;
            return head.next;
        }

        public static ListNode mergeKLists(ListNode[] lists) {
            if (lists == null || lists.length == 0) {
                return null;
            }
            return mergeFromTo(lists, 0, lists.length - 1);
        }

        public static ListNode mergeFromTo(ListNode[] listNodes, int from, int to) {
            if (from == to) {
                return listNodes[from];
            }
            if (to < from) {
                return null;
            }
            int mid = ((to - from) >> 1) + from;
            return mergeTwoLists(mergeFromTo(listNodes, from, mid), mergeFromTo(listNodes, mid + 1, to));
        }

    }
}
